<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="records">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80px"
      ></el-table-column>
      <el-table-column
        label="名称"
        align="center"
        width="250px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        align="center"
        width="250px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="默认图片" align="center" width="250px">
        <template #="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt="糟糕!图片找不到了"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        align="center"
        prop="weight"
        width="250px"
      ></el-table-column>
      <el-table-column
        label="价格(元)"
        align="center"
        prop="price"
        width="250px"
      ></el-table-column>
      <!-- 将操作固定到右侧 -->
      <el-table-column label="操作" align="center" width="400px" fixed="right">
        <template #="{ row, $index }">
          <el-button
            type="info"
            :icon="row.isSale == 1 ? Bottom : Top"
            size="small"
            @click="updateSale(row)"
          />
          <el-button
            type="primary"
            icon="Edit"
            size="small"
            @click="updateSku"
          />
          <el-button
            type="info"
            icon="InfoFilled"
            size="small"
            @click="findSku(row)"
          />
          <el-popconfirm
            width="200"
            :title="`您确定要删除${row.skuName}吗?`"
            @confirm="delSku(row.id)"
          >
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <!-- <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :page-sizes="[100, 200, 300, 400]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="400"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> -->
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getHasSku"
      @size-change="handleSizeChange"
    />
    <!-- 抽屉组件:展示商品详情 -->
    <el-drawer v-model="drawer2" :direction="direction">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <div>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">名称:</el-col>
            <el-col :span="18">{{ skuInfo.skuName }}</el-col>
          </el-row>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">描述:</el-col>
            <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
          </el-row>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">价格:</el-col>
            <el-col :span="18">{{ skuInfo.price }}</el-col>
          </el-row>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                type="danger"
                style="margin: 5px"
                v-for="item in skuInfo.skuAttrValueList"
              >
                {{ item.attrName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                type="success"
                style="margin: 5px"
                v-for="item in skuInfo.skuSaleAttrValueList"
              >
                {{ item.saleAttrName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 10px 0px">
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <el-carousel :interval="4000" type="card" height="200px">
                <el-carousel-item
                  v-for="item in skuInfo.skuImageList"
                  :key="item.id"
                >
                  <img
                    :src="item.imgUrl"
                    alt="糟糕！图片找不到了"
                    style="width: 100%; height: 100%"
                  />
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="cancelClick">cancel</el-button>
          <el-button type="primary" @click="confirmClick">confirm</el-button>
        </div>
      </template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import {
  Top,
  Bottom,
  Edit,
  Delete,
  View,
  InfoFilled,
} from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import {
  reqSkuList,
  reqOnSale,
  reqCancelSale,
  reqSkuInfo,
  reqRemoveSku,
} from '@/api/product/sku/index'
import type {
  SkuResponseData,
  SkuData,
  SkuInfoData,
} from '@/api/product/sku/type'
import { ElMessage, ElMessageBox } from 'element-plus'
const pageNo = ref<number>(1)
const pageSize = ref<number>(10)
const total = ref<number>(0)
const records = ref<SkuData[]>([])
const skuInfo = ref<any>({})
onMounted(() => {
  getHasSku()
})
// 分页器会自动传入currentPage
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  const result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
// v-model已经收集了pageSize
const handleSizeChange = () => {
  getHasSku()
}

const updateSale = async (row: SkuData) => {
  // 上架状态；更新为下架
  if (row.isSale == 1) {
    await reqCancelSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
  } else {
    // 下架状态；更新为上架
    await reqOnSale(row.id as number)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
  }
  // 发请求重新获取已有的SKU
  getHasSku(pageNo.value)
}

const updateSku = () => {
  ElMessage({
    type: 'success',
    message: '程序员在努力的更新中',
  })
}

const findSku = async (row: SkuData) => {
  const result: SkuInfoData = await reqSkuInfo(row.id as number)
  if (result.code == 200) {
    skuInfo.value = result.data
  }
  // 展示抽屉
  drawer2.value = true
}

const delSku = async (id: number) => {
  const result: any = await reqRemoveSku(id)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '删除成功!',
    })
    getHasSku(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: '系统数据不能删除!',
    })
  }
}
const drawer2 = ref<boolean>(false)
// 从右到左
const direction = ref<DrawerProps['direction']>('rtl')
const radio1 = ref('Option 1')
// const handleClose = (done: () => void) => {

//   ElMessageBox.confirm('Are you sure you want to close this?')
//     .then(() => {
//       done()
//     })
//     .catch(() => {
//       // catch error
//     })
// }
function cancelClick() {
  drawer2.value = false
}
function confirmClick() {
  ElMessageBox.confirm(`Are you confirm to chose ${radio1.value} ?`)
    .then(() => {
      drawer2.value = false
    })
    .catch(() => {
      // catch error
    })
}
</script>

<style scoped>
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
